<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

class MY_Controller extends CI_Controller {
	 public function __construct()
    {
		parent::__construct();
        $this->load->model('Product_Model','product');
	
        
    }
    public function layui_encode($data,$count){
    	$a=json_encode($data);
		return '{"code":0,"msg":"","count":'.$count.',"data":'.$a.'}';
		
    }
    // 获取门店id
    public function get_id(){
        return rbac_conf(array('INFO','store_id'));
    }
    // 获取用户昵称
    public function get_nickname(){
        return rbac_conf(array('INFO','nickname'));
    }
     // 获取用户id
    public function get_userid(){
        return rbac_conf(array('INFO','id'));
    }

    // 获取门店规则的值
    public function get_rule($name){
        $mysql="select * from store_rule where store_id = ? and rule_name = ?";
        $res=$this->db->query($mysql,array($this->get_id(),$name))->result();
        foreach ($res as $k => $v) {
            return $v->value;
        }
    }

     // 获取指定字段对应的值
    public function get_value($table,$title,$val,$title1){
        $mysql="select * from $table where store_id = ? and $title = ?";
        $res=$this->db->query($mysql,array($this->get_id(),$val))->row_array();
        return $res[$title1];
    }

    // 通用简洁编辑
    public function my_edit($table,$view,$showdata=''){
        $data=$this->input->post();
        if($data){   //写入
            $id=$data['id'];
            $this->db->update($table,$data,array('id'=>$id));
            $count=$this->db->affected_rows();
            if($count){
                echo '{"status":1, "msg": "数据更新成功"}';
            }else{
                echo '{"status":0, "msg": "没有数据被更新"}';
            }

        }else{  // 输出到页面
            $id=$_GET['id'];
            if($id){
                    $mysql="select * from $table where id =$id";
                    $res=$this->db->query($mysql)->result();
                    $data=$showdata;
                    $data['list']=$this->product->_show_time($res);
                    //var_dump($data);  
                    $this->load->view('head.html');
                    $this->load->view($view,$data);
            }else{
                echo    '{"status":0, "msg": "要修改的数据不存在"}';
            }
        }
        
    }
        //删除
    public function my_del($table){
        $id=$this->input->post('id');

        if($id){
            $mysql="select * from $table where id=$id and isdel=1";
            //查询是否允许删除 1不可删除
            $count=$this->db->query($mysql)->num_rows();
            if($count){
                echo '{"msg":"该条数据不允许删除","status":"0"}';
            }else{
                $this->db->delete($table,array('id'=>$id));
                echo '{"msg":"删除成功","status":"1"}';
            }
        }else{
            echo '{"msg":"数据不存在","status":"0"}';
        }
        
    }
}
